package com.yangyu.controller;

import com.yangyu.message.BaseRsp;
import com.yangyu.message.user.LoginDto;
import com.yangyu.service.UserService;
import com.yangyu.shiro.AccountProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "登录模块")
@RestController
public class AccountController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public BaseRsp login(@RequestBody LoginDto dto, HttpServletResponse response) {
        AccountProfile profile = userService.login(dto, response);
        return new BaseRsp(profile);
    }

    @ApiOperation("登出")
    @RequiresAuthentication
    @PostMapping("/logout")
    public BaseRsp logout() {
        SecurityUtils.getSubject().logout();
        return new BaseRsp();
    }

}
